package com.gc.business.service.impl;

import cn.hutool.core.lang.Console;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gc.business.domain.CommunityManage;
import com.gc.business.domain.CompanyManage;
import com.gc.business.domain.ElevatorInfo;
import com.gc.business.mapper.CommunityManageMapper;
import com.gc.business.service.ICommunityManageService;
import com.gc.business.service.ICompanyManageService;
import com.gc.business.service.IElevatorInfoService;
import com.gc.common.constant.BusinessConstants;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import com.gc.system.domain.SysRole;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 小区管理 服务层实现
 *
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Service
public class CommunityManageServiceImpl extends ServiceImpl<CommunityManageMapper, CommunityManage> implements ICommunityManageService {

    @Autowired
    private ICompanyManageService companyManageService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IElevatorInfoService elevatorInfoService;

    @Override
    public List<CommunityManage> list(CommunityManage communityManage) {
        LambdaQueryWrapper<CommunityManage> wrapper = new LambdaQueryWrapper<>();
        if (communityManage != null) {
            if (StringUtils.isNotEmpty(communityManage.getId())) {
                wrapper.eq(CommunityManage::getId, communityManage.getId());
            }
            if (StringUtils.isNotEmpty(communityManage.getCommunityId())) {
                wrapper.eq(CommunityManage::getCommunityId, communityManage.getCommunityId());
            }
            if (StringUtils.isNotEmpty(communityManage.getCompanyId()) && communityManage.getCompanyId() != 0) {
                wrapper.eq(CommunityManage::getCompanyId, communityManage.getCompanyId());
            }
            if (StringUtils.isNotEmpty(communityManage.getCommunityName())) {
                wrapper.like(CommunityManage::getCommunityName, communityManage.getCommunityName());
            }
            if (StringUtils.isNotEmpty(communityManage.getCommunityLogo())) {
                wrapper.eq(CommunityManage::getCommunityLogo, communityManage.getCommunityLogo());
            }
            if (StringUtils.isNotEmpty(communityManage.getLongitude())) {
                wrapper.eq(CommunityManage::getLongitude, communityManage.getLongitude());
            }
            if (StringUtils.isNotEmpty(communityManage.getLatitude())) {
                wrapper.eq(CommunityManage::getLatitude, communityManage.getLatitude());
            }
            if (StringUtils.isNotEmpty(communityManage.getDetailAddress())) {
                wrapper.like(CommunityManage::getDetailAddress, communityManage.getDetailAddress());
            }
            if (StringUtils.isNotEmpty(communityManage.getCreateBy())) {
                wrapper.like(CommunityManage::getCreateBy, communityManage.getCreateBy());
            }
            if (StringUtils.isNotEmpty(communityManage.getCreateTime())) {
                wrapper.eq(CommunityManage::getCreateTime, communityManage.getCreateTime());
            }
            if (StringUtils.isNotEmpty(communityManage.getUpdateBy())) {
                wrapper.like(CommunityManage::getUpdateBy, communityManage.getUpdateBy());
            }
            if (StringUtils.isNotEmpty(communityManage.getUpdateTime())) {
                wrapper.eq(CommunityManage::getUpdateTime, communityManage.getUpdateTime());
            }
            if (StringUtils.isNotEmpty(communityManage.getRemark())) {
                wrapper.like(CommunityManage::getRemark, communityManage.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 查询政府机构管辖的所有小区列表
     *
     * @param attrId
     * @return
     */
    @Override
    public List<CommunityManage> selectCommunityByAttrId(Long attrId) {

        SysRole sysRole = userService.checkUserRoleByAttrId(attrId);
        //查验身份
        if (sysRole.getRoleKey().equals(BusinessConstants.GOV)) {
            //查询管辖的全部公司列表
            List<CompanyManage> companyList = companyManageService.selectCompanyManageByGovId(attrId, null);
            String companyIds = "";
            for (CompanyManage companyManage : companyList) {
                companyIds = companyIds + companyManage.getCompanyId() + ",";
            }
            Long[] companyArray = Convert.toLongArray(companyIds);
            return baseMapper.selectCompanyManageByCompanyIds(companyArray);
        } else if (sysRole.getRoleKey().equals(BusinessConstants.COMPANY)) {
            CommunityManage communityManage = new CommunityManage().setCompanyId(attrId);
            return this.list(communityManage);
        }
        return null;
    }


    /**
     * 搜索用户查看的小区
     * @param sysUser
     * @param communityManage
     * @return
     */
    @Override
    public List<CommunityManage> searchCommunityList(SysUser sysUser, CommunityManage communityManage,Integer pageNum,Integer pageSize) {
        Long attrId = sysUser.getAttrId();
        List<CommunityManage> communityManageList = new ArrayList<>();
        if (attrId == null || attrId == 0) {
            return communityManageList;
        }
        String roleKey = userService.checkUserRoleByAttrId(attrId).getRoleKey();
        Console.log("roleKey:" + roleKey);
        if (!roleKey.equals(BusinessConstants.COMPANY)) {
            return communityManageList;
        }
        communityManage.setCompanyId(attrId);
        //分页
        if(pageNum != null && pageSize != null){PageHelper.startPage(pageNum,pageSize,"create_time DESC");}
        communityManageList = this.list(communityManage);
        for (CommunityManage manage : communityManageList) {
            List<ElevatorInfo> elevatorInfoList = elevatorInfoService.selectListByCommityId(manage.getCommunityId());
            manage.setElevatorInfoList(elevatorInfoList);
            manage.setElevatorInfoNum(elevatorInfoList.size());
        }
        return communityManageList;
    }
}
